/**
 * createby : JinxLbj
 * date : 2018/8/16
 * desc : 文件传输
 **/

package com.example.demo.common.utils;

import com.example.demo.core.bean.ConfigBean;
import com.jcraft.jsch.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Properties;

@Component
public class SFTPChannel {

    @Autowired
    private ConfigBean configBean;

    private Session session = null;
    private Channel channel = null;

    public void init(String host, Integer port, String ftpUsername, String ftpPassword, String dst) throws JSchException {
        // 创建JSch对象
        JSch jsch = new JSch();
        // 根据用户名，主机ip，端口获取一个Session对象
        session = jsch.getSession(ftpUsername, host, port);
        System.out.println("Session created.");
        if (ftpPassword != null) {
            // 设置密码
            session.setPassword(ftpPassword);
        }
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        // 为Session对象设置properties
        session.setConfig(config);
        // 通过Session建立链接
        session.connect();
        System.out.println("Session connected.");
        System.out.println("Opening Channel.");
        // 打开SFTP通道
        channel = session.openChannel("sftp");
        channel.connect(); // 建立SFTP通道的连接
        System.out.println("Connected successfully to ftpHost = " + host + ",as ftpUserName = " + ftpUsername
                + ", returning: " + channel);
        try {
            System.out.println(">> 建立根文件夹 " + dst);
            ((ChannelSftp) channel).mkdir(dst);
        } catch (Exception e) {
            System.out.println(">> 目录已存在");
        }
    }

    public void closeChannel() throws Exception {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }

    public void upload(String path, String dst) throws SftpException {
        File curPath = new File(path);
        if (curPath.isFile()) {
            System.out.print(">> 传输开始 " + path);
            ((ChannelSftp) channel).put(path, dst);
            System.out.println("      Done !!! ");
            System.out.println(">> 目标文件存放于 " + dst);

        } else {
            File[] childPath = curPath.listFiles();
            if (childPath != null && childPath.length != 0) {
                for (File aChildPath : childPath) {
                    try {
                        ((ChannelSftp) channel).mkdir(dst);
                        System.out.println(">> mkdir " + dst);
                    } catch (Exception e) {
//                        System.out.println(">> 目录已存在");
                    } finally {
                        if (aChildPath.isFile()) {
                            upload(aChildPath.getPath(), dst + File.separator);
                        } else {
                            upload(aChildPath.getPath() + File.separator, dst + File.separator + aChildPath.getName());
                        }
                    }
                }
            }
        }

    }
}
